package com.example.test04

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {

    private val modifyInfoLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ){ result ->
        if (result.resultCode == RESULT_OK){
            val userInfo = result.data?.getSerializableExtra("user_info") as? UserInfo
            userInfo?.let {
                findViewById<TextView>(R.id.name).text = it.name ?: "暂无"
                findViewById<TextView>(R.id.sex).text = if (it.sex == true) "男" else if (it.sex == false) "女" else "暂无"
                findViewById<TextView>(R.id.stuno).text = it.stuno ?: "暂无"
                findViewById<TextView>(R.id.pno).text = it.pno ?: "暂无"
                findViewById<TextView>(R.id.mail).text = it.mail ?: "暂无"
                findViewById<TextView>(R.id.hobby).text = it.hobby ?: "暂无"
            }
        }
    }

    private val gameLouncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ){ result ->
        if(result.resultCode == RESULT_OK){
//            findViewById<TextView>(R.id.max_score).text = result.data?.
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        findViewById<Button>(R.id.modify_info).setOnClickListener(View.OnClickListener{
            val intent = Intent(this, ModifyInfoActivity::class.java)
            modifyInfoLauncher.launch(intent)
        })
        findViewById<Button>(R.id.start_game).setOnClickListener(View.OnClickListener{
            val intent = Intent(this, GameActivity::class.java)
            gameLouncher.launch(intent)
        })
    }
}